package cn.weisoft.redisdemo.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class RedisMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message, byte[] bytes)   {

       /*try{
           Thread.sleep(3000);
       }catch (Exception ex){
           ex.printStackTrace();
       }*/
        System.out.println("process message on onMessage 1:" + new Date());
        String body = new String(message.getBody());
        String topic = new String(bytes);
        System.out.println("body:" + body);
        System.out.println("topic:" + topic);

       /* ObjectMapper mapper = new ObjectMapper();
        StringRedisSerializer  stringRedisSerializer = new StringRedisSerializer();
        try {

            String body2 = stringRedisSerializer.deserialize(message.getBody());
            System.out.println("body2:" + body2);
            body = body.substring(body.indexOf("{"));

            Book book = Book.parseFromJson(body);

            System.out.println(book.toString());

        } catch (Exception ex) {
            ex.printStackTrace();
        }*/

    }
}
